######################################################/
## Neither a Trait nor Wildly Fluctuating: On the    ##
## Stability of Populist Attitudes and its           ##
## Implications for Empirical Research               ##
## -- Replication Material (Read Me)                 ##
## by: Schimpf, Wuttke, Schoen                       ##
## Version: June 2023                                ##
######################################################/

#########################################################################################
#### /// Overview                                                                   #####
#########################################################################################

# This READ ME.txt file provides the necessary information to replicate the
# analyses for the paper "Neither a Trait nor Wildly Fluctuating: On the 
# Stability of Populist Attitudes and its Implications for Empirical 
# Research" pubblished in the British Journal of Political Science.

# The replication materials include a dataset ("df_GLES17_recoded.csv"), four
# R-Scripts, a Stata DoFile and this txt file. The replication of the
# analysis based on the recoded dataset requires R, processing the 
# original GLES data requires STATA.

#########################################################################################
#### /// Do-Files and R-Scripts - content and function                              #####
#########################################################################################

# All recodes, analyses, etc. can be run from the Master R-Script after changing
# the working directory in line 13 of the Masterfile. The
# following provides an overview of the Stata DoFile and R-Scripts and their role in the 
# replication process:

# 1) STATA_GLESPanelData_VarSelection_072021_Schimpfetal2023: This DoFile was used after
running the pre-processing DoFile provided with the original dataset (see below) to limit 
the dataset used to work with in R.

# 2) R_Script_Masterfile_Schimpfetal2023.R: Use this Script to run the remaining
R-Scripts. NOTE that, in order to replicate the analyses, a folder containing all Scripts
(for more, see below) needs to be set up and the working directory needs to be set accordingly
(Line 13 in the Masterfile)

# 3) RScript_Recode_062023_Schimpfetal2023.R: This R-Script contains all the data wrangling
to generate variables used in the analyses based on the original GLES data. If run, it also
produces the dataset with the transformed/generated variables supplied here to reproduce 
the final analyses (for more details, see below).

# 4A) R_Script_Invariance_Tests_Schimpfetal2023.R: Contains the R-Code to replicate the 
invariance analyses we conducted.

# 4B) RScript_Analyses_062023_Schimpfetal2023.R: This R-Script contains all remaining analyses.
If run from the Masterfile and with the working directory set correctly, it will also 
produce all figures from the main text and appendix and save them in the folder that 
contains the replication materials. NOTE: This R-Script will call results from the 
Invariance Test. To avoid error messages, run the invariance test script first.

#########################################################################################
#### /// Base Dataset - GLES Data Source                                            #####
#########################################################################################
    
## The dataset we used here is the GLES Panel Dataset Version 5.0.0. We 
## accessed the file on July 24, 2021. Full source:
##  GLES (2021): GLES Panel 2016-2021, Waves 1-15. GESIS Data Archive, 
##  Cologne. ZA6838 Data file Version 5.0.0,  https://doi.org/10.4232/1.13783

#########################################################################################
#### /// Replicating Data Wrangling                                                #####
#########################################################################################

# NOTE: To merely replicate the analyses, it is NOT necessary to go through the whole 
# data wrangling process. Skip to next section for more


# To replicate the full data wrangling process, proceed in the following steps 

# 1) Download the original dataset (see previous section)

# 2) Run the DoFile supplied by the GLES Project to merge the separate datasets (they
# come separately for different waves and components) and to adjust some ordering. The DoFile
# is called "ZAA6838_Wave_Match_v5-0-0". The DoFile requires that you need to select the 
# relevant sample components. Our selection criteria were set to the following:

/* Specify sample(s) you want to include. */
local sample_select `"A"' /* Select from: "A" , "A4", "B",  or "AB" */

/* Specify waves you want to include */
local wave_select `"all"' /* Select from: "all", "10", "11", ...  */

# 3) Run the Stata DoFile provided as part of our replication material. The File will
select the relevant variables on the basis of the dataset generated in #2 and save 
an outcome file as csv. Place that csv file in the folder with all the other materials
(see section "Replicating the Data Analyses"). Use this DoFile to add additional variables
from the base dataset if required.

# 4) Run the R-Script for recoding/data wrangling from the R Masterscript. Before running
 the R-Script, adjust the working directory (Line 13 in the Masterfile). The script
will produce a csv file called "df_GLES17_recoded.csv" (identical to the dataset provided
as part of the replication materials - see next section).

#########################################################################################
#### /// Analyses replication dataset                                               #####
#########################################################################################

# The replication files contain the following csv dataset:
# df_GLES17_recoded.csv
# This file contains the original running ID of the GLES dataset plus all the variables
# generated through the data wrangling process. Using this dataset is sufficient
# to replicate the full analyses (see next section). One can use the dataset provided by
# us or create their own following the data wrangling process in the previous section.

#########################################################################################
#### /// Replicating the Data Analyses                                              #####
#########################################################################################

# To replicate the data analyses, proceed as follows:

# 1) Create folder ("/PopStabilityReplication") and place all scripts and the 
#   dataset "df_GLES17_recoded.csv" in the folder.

# 2) Adjust the working directory in Line 13 of the Master R-Script (if you have not yet
#    done so). 

# 3) Run the following two R-Scripts from the Masterfile:
# - R_Script_Invariance_Tests_Schimpfetal2023.R

# - RScript_Analyses_062023_Schimpfetal2023.R (NOTE: This R-Script will call results
from the Invariance Test. To avoid error messages, run invariance test script first.)


## Good luck!